<?php

namespace App\Http\Controllers\Passenger\V2\Order;


use App\Http\Controllers\Controller;
use App\Models\Orders;
use App\Models\PayOrder;
use App\Models\RefundOrder;
use App\Models\UserCoupon;
use App\Services\PassengerPayService;
use App\Services\PassengerUserCouponService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

/**
 * 支付相关
 * Class PayController
 * @package App\Http\Controllers\Passenger\V2\Order
 */
class PayController extends Controller
{


    /**
     * 创建预支付信息
     * @param Request $request
     */
    public function create(Request $request, PassengerPayService $service)
    {

        $order_id = (int)$request->post('id');

        $order_info = Orders::query()->find($order_id);

        if (!$order_info || $order_info->user_id != Auth::id())
            return $this->sendError('订单ID有误');

        return $this->sendJson(
            $service->getPayData($order_info)
        );
    }

    /**
     * 余额支付
     */
    public function BalancePayment(Request $request, PassengerPayService $service)
    {
        $order_id = (int)$request->post('id');
        if ($service->BalancePayment($order_id)) {
            return $this->sendSuccess('支付成功');
        }
        return $this->sendError('支付失败');
    }

    /**
     * 生成支付订单
     */
    public function createSuccess(Request $request)
    {
        $order_id = (int)$request->post('id');
        $user_coupon_id = (int)$request->post('user_coupon_id', 0);

        $order_info = Orders::query()
            ->where('status', '4')
            ->where('user_id', Auth::id())
            ->find($order_id);
        if (!$order_info || $order_info->real_should_price !== null)
            return $this->sendError('订单ID有误');

        DB::beginTransaction();

        if ($user_coupon_id) {

            $user_coupon_info = UserCoupon::query()
                ->where('status', 1)
                ->where('user_id', Auth::id())
                ->where(function ($query) use ($order_info) {
                    $query->where('overdue_time', 0)->orWhere('overdue_time', '>', strtotime($order_info->distance->book_time));
                })
                ->find($user_coupon_id);

            if (!$user_coupon_info)
                return $this->sendError('用户优惠券ID有误');

            $passenger_user_coupon_service = new PassengerUserCouponService();
            $coupon_list = $passenger_user_coupon_service->getListAllByOrder($order_info->service_type_id, $order_info->cost_price, $order_info->distance->book_time);

            if (!$coupon_list || !in_array($user_coupon_id, array_column($coupon_list, 'id')))
                return $this->sendError('用户优惠券ID有误');

        }

        $passenger_pay_service = new PassengerPayService();
        $passenger_pay_service->createByOrderSuccess($order_info, $user_coupon_id);

        $return = [];
        $pay_order = PayOrder::query()->where('order_id', $order_info->id)->where('status', 0)->first();
        if ($pay_order) { //待支付
            $return['pay_price'] = $pay_order->price;
            $return['pay_id'] = $pay_order->id;
        }

        $refund_order = RefundOrder::query()->where('order_id', $order_info->id)->where('status', 0)->first();
        if ($refund_order) { //退款
            $return['refund_price'] = $refund_order->price;
            $return['refund_id'] = $refund_order->id;
        }

        DB::commit();

        return $this->sendJson($return);
    }

}
